home *** CD-ROM | disk | FTP | other *** search
- Path: mayne.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: How to read a STRING from file?
- Date: 15 Mar 1996 14:28:33 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4icquhINNt76@mayne.ugrad.cs.ubc.ca>
- References: <s3032089.13.314913E2@sparc13.ncu.edu.tw>
- NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
-
- In article <s3032089.13.314913E2@sparc13.ncu.edu.tw>,
- Alexander PeaceLand <s3032089@sparc13.ncu.edu.tw> wrote:
- >How could I read a real string from the file?
- >I used to read string by fscanf(), but it seems not work when the string
- >I want is like "This is a book" that with space within it.
- >Is there any function or easy way to read sentence from file?
-
- You can read newline-terminated sequences as strings using fgets().
-
- To read zero-terminated data from a file, you will probably want to use fgetc()
- to scan individual cahracters from the file until you detect a zero. Same with
- reading a sentence. If your criteria for what a sentence is define it as the
- shortest available sequence of characters terminated by a period, you can do it
- easily using:
-
- while(1) { /* loop indefinitely */
- char c = fgetc(inputfile); /* get next characte */
- if (c == EOF) /* if end of file */
- break;
- /* else record the character */
-
- *bufptr++ = c; /* should check for buffer overflow */
- /* or change the design to allow */
- /* arbitrary length sentences */
- if (c == '.'); /* bail if at end of sentence */
- break;
- }
- *bufptr++ = '\0'; /* zero-terminate the string */
-
- Of course these criteria for a sentence are naive. An abbreviation such as Mr.
- in the middle of a sentence will defeat it easily. But it's about the best you
- can do with zero lookahead.
- --
-
-